package com.ZeroCarbon.service.impl;

import com.ZeroCarbon.domain.dto.QuestionInfoDTO;
import com.ZeroCarbon.domain.po.QuestionInfo;
import com.ZeroCarbon.exception.BusinessLogicException;
import com.ZeroCarbon.mapper.QuestionMapper;
import com.ZeroCarbon.service.IQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import static com.ZeroCarbon.exception.BusinessLogicException.ERROR_EXCEPTION;
import static com.ZeroCarbon.prefix.RedisKey.STRING;
import static com.ZeroCarbon.utils.BusinessMethodUtils.isExist;

/**
 * 密保信息实现类
 *
 * @since 2024-10-15
 */
@Service
@RequiredArgsConstructor
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, QuestionInfo> implements IQuestionService {

    /**
     * 保存用户密保信息
     */
    @Override
    public void saveQuestion(QuestionInfoDTO questionInfoDTO) throws BusinessLogicException {
        //转化对象
        QuestionInfo questionInfo = questionInfoDTO.toQuestionInfo();
        //保存对象
        try {
            if (!save(questionInfo)) {
                throw new BusinessLogicException();
            }
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
    }

    /**
     * 获取用户密保信息
     */
    @Override
    public QuestionInfoDTO getQuestion(String id) throws BusinessLogicException {
        //若相关账号未注册则退出
        if (isExist(STRING, id)) {
            return null;
        }
        //记录密保信息
        QuestionInfo questionInfo;
        try {
            questionInfo = getById(id);
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
        //转化对象
        return questionInfo.toQuestionInfoDTO();
    }
}
